<?php


namespace App\Http\Admin\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use App\Role;
use App\Http\Authentication\UserAuthentication;

class RolesController  extends Controller{

    use UserAuthentication;



    /*
   * 添加角色
   * */


    public function add_role(Request $request){




        $data=$request->only("name","display_name",'description','perms');

        if(!preg_match('/^[a-zA-Z\-0-9]{1,20}$/',$data['name']))  return ThrowApiExc('31001');
        if(!preg_match('/^[0-9\[\]\,]{1,200}$/',$data['perms']))  return ThrowApiExc('31001');
//        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,10}$/u',$data['display_name'])) return ThrowApiExc('31002');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,30}$/u',$data['description'])) return ThrowApiExc('31002');

        $perms = json_decode($data['perms']);
        $role = new Role();
        $role -> name = $data['name'];
        $role->display_name = $data['display_name'];
        $role->description = $data['description'];
        $role->save();
        $role->attachPermissions($perms);


        return [
            'status_code'      => 200,
            'message'     =>  '提交成功',
            'data' => []
        ];

    }


    /*
     * 修改角色
     * */

    public function update_role(Request $request,$roleId){



        $data=$request->only("name","display_name",'description','perms');

        if(!preg_match('/^[a-zA-Z\-0-9]{1,20}$/',$data['name']))  return ThrowApiExc('31001');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,10}$/u',$data['display_name'])) return ThrowApiExc('31002');
//        if(!preg_match('/^[0-9\,]{1,200}$/',$data['perms']))  return ThrowApiExc('31001');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,30}$/u',$data['description'])) return ThrowApiExc('31002');
        $perms = json_decode($data['perms']);
        $role = new Role();
        $role = $role->where("id",$roleId)->first();
        $role -> name = $data['name'];
        $role->display_name = $data['display_name'];
        $role->description = $data['description'];

        $role->save();

        $role->detachPermissions($role->cachedPermissions());
        $role->attachPermissions($perms);

        return [
            'status_code'      => 200,
            'message'     =>  '修改成功',
            'data' => []
        ];

    }


    /*
     * 查看角色
     * */

    public function select_role(){

        //获取全部角色
        $role = new Role();
        $res = $role->get();
        return [
            'status_code'      => 200,
            'message'     =>  '提交成功',
            'data' => $res
        ];
    }

    /*
     * 查看角色的权限
     * */

    public function select_role_permission($permission){



        $role = new Role();
        $role = $role->where("id",$permission)->first();
        $res = $role->cachedPermissions();


        return [
            'status_code'      => 200,
            'message'     =>  '提交成功',
            'data' => $res
        ];
    }

    /*
     * 删除角色
     * */

    public function delete_role($roleId){



        $role = new Role();
        $role->where("id",$roleId)->delete();
        return [
            'status_code'      => 200,
            'message'     =>  '删除成功',
            'data' => []
        ];

    }

}
